package com.example.yuntiku.dao.impl;

import com.example.yuntiku.dao.BaseDao;
import com.example.yuntiku.dao.RecordDao;
import com.example.yuntiku.entity.Record;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RecordDaoImpl extends BaseDao implements RecordDao {
    public int updateRecordlist(int recordlist,String knowledgeid){
        String sql = "UPDATE `yuntiku`.`record` SET `recordlist` = CONCAT(`recordlist`,',',?) WHERE `knowledgeid` = ?;";
        return super.executeUpdate(sql,recordlist,knowledgeid);
    }
    public int insertRecord(Record record){
        String sql = "INSERT INTO `yuntiku`.`record` (`section`,`knowledgeone`,`knowledgeid`,`percentum`,`sid`,`recordlist`)\n" +
                "VALUES(?,?,?,?,?,?);";
        return super.executeUpdate(sql,record.getSection(),record.getKnowledgeone(),record.getKnowledgeid(),record.getPercentum(),record.getSid(),record.getRecordlist());
    }
    public int selectRecord(int knowledgeid){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT COUNT(*) AS `jlshu` FROM `record` WHERE `knowledgeid`=?;");
            stmt.setInt(1,knowledgeid);
            rs = stmt.executeQuery();
            while (rs.next()){
                int jlshu = rs.getInt("jlshu");
                return jlshu;
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return 0;
    }
    public Record selectTmu(int knowledgeid){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT `rid`,`section`,`knowledgeone`,`knowledgeid`,`percentum`,`sid`,`recordlist` FROM `record` WHERE `knowledgeid`=?");
            stmt.setInt(1,knowledgeid);
            rs = stmt.executeQuery();
            while (rs.next()){
                Record record = new Record();
                record.setRid(rs.getInt("rid"));
                record.setSection(rs.getInt("section"));
                record.setKnowledgeone(rs.getInt("knowledgeone"));
                record.setKnowledgeid(rs.getInt("knowledgeid"));
                record.setPercentum(rs.getInt("percentum"));
                record.setSid(rs.getInt("sid"));
                record.setRecordlist(rs.getString("recordlist"));
                return record;
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return null;
    }
    public int updateCompleteness(int percentum,int knowledgeid){
        String sql = "UPDATE `yuntiku`.`record` SET `percentum` = ? WHERE `knowledgeid` =?;";
        return super.executeUpdate(sql,percentum,knowledgeid);
    }

    public int selectJindu(int knowledgeid){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT `percentum` FROM `record` WHERE `knowledgeid`=?");
            stmt.setInt(1,knowledgeid);
            rs = stmt.executeQuery();
            while (rs.next()){
                return rs.getInt("percentum");
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return 0;
    }
}
